#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
快速配置脚本
一键配置DeepSeek或OpenAI API
"""

import os
import json
from pathlib import Path

def quick_setup():
    """快速配置API"""
    print("云顶之弈智能助手 - 快速配置")
    print("=" * 40)

    print("\n请选择API提供商：")
    print("1. DeepSeek (推荐，性价比高)")
    print("2. OpenAI")
    print("3. 查看当前配置")
    print("4. 退出")

    choice = input("\n请选择 (1-4): ").strip()

    if choice == "1":
        setup_deepseek()
    elif choice == "2":
        setup_openai()
    elif choice == "3":
        show_current_config()
    elif choice == "4":
        print("再见！")
        return
    else:
        print("无效选择")
        quick_setup()

def setup_deepseek():
    """配置DeepSeek"""
    print("\n=== DeepSeek API配置 ===")
    print("1. 访问 https://platform.deepseek.com/")
    print("2. 登录并获取API密钥")

    api_key = input("\n请输入DeepSeek API密钥: ").strip()

    if not api_key:
        print("未输入密钥")
        return

    # 配置信息
    config = {
        "openai_api_key": api_key,
        "openai_api_base": "https://api.deepseek.com",
        "model": "deepseek-chat",
        "temperature": 0.1
    }

    # 保存配置
    project_root = Path(__file__).parent
    config_file = project_root / "config.json"

    with open(config_file, 'w', encoding='utf-8') as f:
        json.dump(config, f, indent=2, ensure_ascii=False)

    print(f"\n✓ DeepSeek配置已保存到 {config_file}")
    print("\n现在可以启动助手了：")
    print("python start_deepseek.py")

def setup_openai():
    """配置OpenAI"""
    print("\n=== OpenAI API配置 ===")
    print("1. 访问 https://platform.openai.com/api-keys")
    print("2. 登录并获取API密钥")

    api_key = input("\n请输入OpenAI API密钥: ").strip()

    if not api_key:
        print("未输入密钥")
        return

    # 配置信息
    config = {
        "openai_api_key": api_key,
        "model": "gpt-3.5-turbo",
        "temperature": 0.1
    }

    # 保存配置
    project_root = Path(__file__).parent
    config_file = project_root / "config.json"

    with open(config_file, 'w', encoding='utf-8') as f:
        json.dump(config, f, indent=2, ensure_ascii=False)

    print(f"\n✓ OpenAI配置已保存到 {config_file}")
    print("\n现在可以启动助手了：")
    print("python start_simple.py")

def show_current_config():
    """显示当前配置"""
    print("\n=== 当前配置 ===")

    project_root = Path(__file__).parent
    config_file = project_root / "config.json"

    if config_file.exists():
        try:
            with open(config_file, 'r', encoding='utf-8') as f:
                config = json.load(f)

            api_key = config.get('openai_api_key', '')
            api_base = config.get('openai_api_base', '')
            model = config.get('model', '')

            print(f"API密钥: {'已配置' + ' (' + api_key[:10] + '...)' if api_key else '未配置'}")
            print(f"API基础URL: {api_base if api_base else '默认'}")
            print(f"模型: {model if model else '默认'}")

            # 判断API类型
            if 'deepseek.com' in api_base:
                print("\n检测到: DeepSeek API配置")
                print("启动命令: python start_deepseek.py")
            elif model and 'deepseek' in model:
                print("\n检测到: DeepSeek API配置")
                print("启动命令: python start_deepseek.py")
            elif api_key:
                print("\n检测到: OpenAI API配置")
                print("启动命令: python start_simple.py")
            else:
                print("\n未检测到有效配置")

        except Exception as e:
            print(f"配置文件读取错误: {e}")
    else:
        print("未找到配置文件")
        print("请先配置API密钥")

    input("\n按回车键继续...")

if __name__ == "__main__":
    try:
        quick_setup()
    except KeyboardInterrupt:
        print("\n程序被用户中断")
    except Exception as e:
        print(f"程序异常: {e}")